package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Employee;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class EDao {

	DBHelp<Employee> db=new DBHelp<Employee>();
	
	public void save(String username){
		String sql="insert into t_employee(username) values(?)";
		db.executeSQL(sql,username);
	}
	public void delete(String username){
		String sql="delete from t_employee where username=?";
		db.executeSQL(sql,username);
	}
	public void update(Employee emp){
		String sql="update t_employee set username=?,password=? where id=?";
		db.executeSQL(sql,emp.getUsername(),emp.getPassword(),emp.getId());
	}
	public Employee findByNameAndPassword(String username,String password){
		String sql="select id,username,password from t_employee where username=? and password=?";
		return db.queryForObject(sql,new ERowMapper(),username,password);
	}
	public Employee findByName(String username){
		String sql="select id,username,password from t_employee where username=?";
		return db.queryForObject(sql,new ERowMapper(),username);
	}
	public Employee findById(int id){
		String sql="select id,username,password from t_employee where id=?";
		return db.queryForObject(sql,new ERowMapper(),id);
	}
	public List<Employee> findAll(){
		String sql="select id,username,password from t_employee";
		return db.queryForList(sql,new ERowMapper());
	}
	private class ERowMapper implements RowMapper<Employee>{
		
		public Employee mapperRow(ResultSet rs) throws SQLException{
			Employee emp=new Employee();
			emp.setId(rs.getInt("id"));
			emp.setPassword(rs.getString("password"));
			emp.setUsername(rs.getString("username"));
			return emp;
		}
	}
	
}
